package com.calf.module.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by jinhui.li on 2016/4/28.
 */
public class DateUtil {

    private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    
	public static Date stringTODateNow(String time){
		Date dateTime=null;
		try {
	        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	        Date date = simpleDateFormat.parse(time);
	        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String formattedTime = simpleDateFormat.format(date);
	         dateTime=simpleDateFormat.parse(formattedTime);
	        System.out.println("---->将" + time + "解析为:" + dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateTime;
	}
	
//	155630350205   wuzhiguoyy   08632657


    
    public static Date stringToDate(String date) {
        return stringToDate(date, DEFAULT_PATTERN);
    }

    public static Date stringToDate(String date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {

        }
        return null;
    }

    public static String dateToString(Date date) {
        return dateToString(date, DEFAULT_PATTERN);
    }

    public static String dateToString(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
    
   /**获取当前系统时间返回Date**/
    public static Date getCurrentDate(){
        return new Date();
    }
    /**获取系统当前时间返回字符串**/
    public static String getCurrentDateStr(){
        return dateToString(getCurrentDate());
    }

    public static String getCurrentDateStr(String pattern){
        return dateToString(getCurrentDate(), pattern);
    }
    
    /**字符串转化Date
     * @throws ParseException **/
    
    public static Date getDate(String dateDay) throws ParseException{
    	SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);  
        Date date;
			date = sdf.parse(dateDay);
		return date;
    }
    
    /* 
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_PATTERN);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

}
